/****************************************************
	文件：NewBehaviourScript.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/12/20 21:48:17
	功能：
*****************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using HTFW.Consts;
using HTFW.Core;
using HTFW.Res;
using HTFW.Logger;
using HTFW.Scene;
using HTFW.Scheme;
using HTFW.UI;
using HTFW.Utility.Global;
using UnityEngine;
using Object = UnityEngine.Object;

namespace HTFW.Res
{
    public class NewBehaviourScript : MonoBehaviour
    {
        //*
        private HTDriver mDriver;
        private UnitContext mTestContext;

        private void Awake()
        {
            mDriver = new HTDriver(this);
            mDriver.Init();
            mTestContext = HTDriver.GlobalContext;
        }

        private Sprite mSp;

        private IRoutineHandle mRoutine;

        IEnumerator TestCorou()
        {
            int index = 0;
            while (true)
            {
                HLog.Debug(index.ToString());
                index++;
                yield return new WaitForSeconds(1f);
            }
        }

        IEnumerator TestC(string name)
        {
            yield return new WaitForSeconds(3f);
            Debug.Log(name);
        }

        private int tid;

        private void Update()
        {
            mDriver.OnLogicUpdate();
            mDriver.OnViewUpdate();
            mDriver.OnTimerUpdate();
            mDriver.OnLateUpdate();

            if (Input.GetKeyDown(KeyCode.R))
            {
                //mRoutine = HTDriver.GlobalContext.CoroutineContext.Start(TestCorou());
                tid = HTDriver.GlobalContext.CoroutineContext.Start(TestC("111"));
            }

            if (Input.GetKeyDown(KeyCode.A))
            {
                HTDriver.GlobalContext.CoroutineContext.Stop(tid);
                // mRoutine.Start();
            }

            if (Input.GetKeyDown(KeyCode.F))
            {
                //mRoutine.Dispose();
            }

            if (Input.GetKeyDown(KeyCode.Z))
            {
                HTDriver.GlobalContext.CoroutineContext.Dispose();
            }

            //return;


            if (Input.GetKeyDown(KeyCode.Space))
            {
                var view = UIManager.Instance.ShowWindow<LoadingView>();
                var model = new LoadingModel();
                var ctrl = new LoadingController(view, model);
                ctrl.Init();
                SceneManager.Instance.LoadScene(SceneType.Login, () => { HLog.Debug("卸载资源"); }, () =>
                {
                    HTDriver.GlobalContext.ResContext.RObject.PreloadSync<Sprite>("Assets/GameData/Sprites/Item0.png",
                        true);
                    HLog.Debug("预加载资源");
                }, () =>
                {
                    UIManager.Instance.CloseWindow<LoadingView>();
                    ctrl.Dispose();
                    HLog.Debug("加载场景完毕");
                });
            }

            if (Input.GetKeyDown(KeyCode.W))
            {
                EquipmentScheme data = SchemeManager.Instance.Load<EquipmentScheme>(E_Scheme.Equipment);
                HLog.Debug(data.EquipmentList[0].Description);
            }

            if (Input.GetKeyDown(KeyCode.A))
            {
                mSp = mTestContext.ResContext.RObject.LoadSync<Sprite>("Assets/GameData/Sprites/Item0.png", true);
            }

            if (Input.GetKeyDown(KeyCode.B))
            {
                mTestContext.ResContext.RObject.LoadAsync<Sprite>("Assets/GameData/Sprites/Item0.png",
                    ResTaskPriority.High,
                    (obj) =>
                    {
                        mSp = obj;
                        HLog.Debug(mSp.name);
                    }, true);
            }

            if (Input.GetKeyDown(KeyCode.C))
            {
                mTestContext.ResContext.RObject.PreloadSync<Sprite>("Assets/GameData/Sprites/Item0.png", true);
            }

            if (Input.GetKeyDown(KeyCode.D))
            {
                mTestContext.ResContext.RObject.PreloadAsync<Sprite>("Assets/GameData/Sprites/Item0.png",
                    ResTaskPriority.High,
                    (obj) =>
                    {
                        mSp = obj;
                        HLog.Debug(mSp.name);
                    }, true);
            }

            if (Input.GetKeyDown(KeyCode.E))
            {
                mTestContext.Dispose();
            }

            if (Input.GetKeyDown(KeyCode.Q))
            {
                mTestContext.ResContext.RObject.Unload(mSp, true);
            }
        }

        public void Init(ResTaskPriority priority)
        {
            // ResData res = ResClassObjectPool.Instance.SpawnResData();
            // res.Path = "Assets/GameData/Prefabs/Test1.prefab";
            //
            // res.Priority = priority;
            //
            // res.AssetName = "Test1.prefab";
            //
            // res.ABName = "test1";
            //
            // res.DependencyABNameList = new List<string>();
            //
            // res.DependencyABNameList.Add("sprites");
            //
            // res.ResType = ResType.Asset;
            //
            // res.Init();
            //
            // res.StartLoadTime = DateTime.Now.Ticks;
            //
            // AssetTask task = ResTaskFactory.Instance.CreateAssetTask(res);
            //
            // mResult = task.LoadAsync((selfRes) =>
            // {
            //     selfRes.EndLoadTime = DateTime.Now.Ticks;
            //     Debug.Log($"load done.  {selfRes.LoadTime}"); 
            // });

            //mResult = ResMgr.Instance.LoadAsync<GameObject>("Assets/GameData/Prefabs/Test1.prefab", priority,(obj) => { Instantiate(obj); });

            //mResult = ResMgr.Instance.LoadAsync<Sprite>("Assets/GameData/Sprites/Item0.png", priority, (obj) => { HLog.Debug($"obj {obj.name} loaded"); }, true);

            //mTestObj = RPrefab.Instance.Instantiate("Assets/GameData/Prefabs/Test1.prefab");
        }

        private ILoadResult mResult;
        private GameObject mTestObj;

        public void Dispose()
        {
        }

        IEnumerator Cor()
        {
            int i = 0;
            while (true)
            {
                i++;
                if (i % 2 == 0)
                {
                    Debug.Log(i);
                    yield return new WaitForSeconds(2f);
                    Debug.Log("aa");
                    continue;
                }

                Debug.Log("bb");
            }
        }

        //*/
    }
}